package com.wl.cloud.system.controller;

import com.wl.cloud.core.dto.RestResultDTO;
import com.wl.cloud.system.service.SysCommonService;
import com.wl.cloud.system.support.dto.LoginDTO;
import com.wl.cloud.system.support.dto.RegisterDTO;
import com.wl.cloud.system.support.utils.ConstantSystem;
import com.wl.cloud.system.support.vo.LoginVO;
import com.wl.cloud.system.support.vo.SysUserVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author: wanglin
 * @date: 2023-10-23 周一
 * @Version: 1.0
 * @Description:
 */
@Tag(name = "公共接口", description = "CommonController")
@Validated
@CrossOrigin
@RestController
@RequestMapping(value = ConstantSystem.PROJECT_URL_PREFIX + "sys/common")
public class CommonController {

    @Autowired
    private SysCommonService commonService;

    @Operation(summary = "注册")
    @PostMapping(value = "register")
    public RestResultDTO<Void> register(@Validated @RequestBody RegisterDTO dto) {
        commonService.register(dto);
        return RestResultDTO.newSuccess();
    }

    @Operation(summary = "登录")
    @PostMapping(value = "login")
    public RestResultDTO<LoginVO> login(@Validated @RequestBody LoginDTO dto) {
        return commonService.login(dto);
    }

    @Operation(summary = "退出登录")
    @GetMapping("logout")
    public RestResultDTO<Void> logout(HttpServletRequest request) {
        commonService.logout(request);
        return RestResultDTO.newSuccess();
    }

    @Operation(summary = "刷新token")
    @GetMapping("refreshToken")
    public RestResultDTO<LoginVO> refreshToken(HttpServletRequest request) {
        return commonService.refreshToken(request);
    }

    //    @PreAuthorize("hasAnyAuthority('ADMIN')")
    @Operation(summary = "重置密码——管理员")
    @RequestMapping(value = "resetPassword", method = {RequestMethod.PUT, RequestMethod.POST})
    public RestResultDTO<Void> resetPassword(@Parameter(description = "用户Id") String userId, @Parameter(description = "重置密码") String password) {
        commonService.resetPassword(userId, password);
        return RestResultDTO.newSuccess();
    }

    @Operation(summary = "获取用户信息")
    @RequestMapping(value = "getInfo", method = {RequestMethod.POST, RequestMethod.GET})
    public RestResultDTO<SysUserVO> getInfo(HttpServletRequest request) {
        return RestResultDTO.newSuccess(commonService.getInfo(request));
    }
}

